import print from 'vue3-print-nb'
import store from '@/store'

function checkPermission (el, { value }) {
  if (value && Array.isArray(value)) {
    let points = []
    try {
      // 获取对应权限
      points = store.getters.user.permission.points
    } catch (e) {
      points = []
    }
    // 匹配对应指令
    const checked = points.some(item => value.includes(item))
    // 匹配失败，删除对应元素
    if (!checked) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    throw new Error('v-permission value must be ["user"...]')
  }
}

const permission = {
  mounted: checkPermission,
  updated: checkPermission
}

export default app => {
  app.use(print)
  app.directive('permission', permission)
}
